package com.mohansever.controller;


import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mohansever.service.UserService;
import com.mohansever.util.ErrStatusEnum;
import com.mohansever.util.PageData;
import com.mohansever.util.ResponseUtil;

@Controller
@RequestMapping(value = "/v1/wx")
public class UserController extends BaseController{
	@Resource
	private UserService userService;
	
	/**
	 * 微信登录
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object login(HttpServletRequest request,@RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"code"}, pd)) {
				repUtil = userService.wxLogin(pd);
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {  
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		return returnObject(pd, repUtil);
	}
	
	/**
	 * 添加微信用户信息
	 * @param request
	 * @param parmMap
	 * @return
	 */
	@RequestMapping(value = "/addUser", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object wxUserInfo(HttpServletRequest request, @RequestBody Map<String, Object> parmMap) {
		PageData pd = new PageData(parmMap);
		ResponseUtil repUtil = new ResponseUtil();
		try {
			if (validataParms(new String[]{"openId","nickName","avatarUrl","gender","country","province","city","language"}, pd)) {
				repUtil = userService.wxAddUserInfo(pd);
			}else{
				repUtil.fail(ErrStatusEnum.PARMERR.getCode(), ErrStatusEnum.PARMERR.getMessage());
			} 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			repUtil.fail(ErrStatusEnum.SYSERR.getCode(), ErrStatusEnum.SYSERR.getMessage());
		}
		
		return returnObject(pd, repUtil);
	}
}
